package com.ruoyi.basebill.service.impl.billIssue;

import com.ruoyi.basebill.domain.*;
import com.ruoyi.basebill.mapper.TWfzRemunerationMapper;
import com.ruoyi.basebill.service.IWfzGzffService;
import com.ruoyi.basebill.service.IWfzJtgzflService;
import com.ruoyi.basebill.service.billIssue.IPushGzclService;
import com.ruoyi.basebill.service.exception.DataNullException;
import com.ruoyi.basebill.service.impl.*;
import com.ruoyi.basebill.service.impl.billIssue.helper.GzftHelper;
import com.ruoyi.basebill.service.impl.billIssue.helper.GzjtHelper;
import com.ruoyi.basebill.service.impl.billIssue.util.BillIssueUtil;
import com.ruoyi.basebill.service.impl.billIssue.util.GzUtil;

import com.ruoyi.basedata.domain.*;
import com.ruoyi.basedata.mapper.WfzBankMapper;
import com.ruoyi.basedata.service.IWfzBankService;
import com.ruoyi.basedata.service.IWfzBaseunitService;
import com.ruoyi.basedata.domain.WfzPerson;

import com.ruoyi.basedata.service.impl.WfzPersonServiceImpl;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.math.BigDecimal;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @projectName: wfz
 * @package: com.ruoyi.basebill.service.impl.billIssue
 * @className: pushGzff
 * @author: wenyx
 * @description: TODO
 * @date: 2023/1/10 14:43
 * @version: 1.0
 */
@Service
public class PushGzclServiceImpl implements IPushGzclService {
    //薪酬总表-工资发放服务层接口
    @Autowired
    private TWfzRemunerationMapper tWfzRemunerationMapper;
    //单据下发工具类
    @Autowired
    private BillIssueUtil billIssueUtil;
    //工资发放服务层接口
    @Autowired
    private IWfzGzffService wfzGzffService;
    //工资分摊服务层接口
    @Autowired
    private WfzGzftServiceImpl wfzGzftService;
    //工资计提服务层接口
    @Autowired
    private WfzGzjtServiceImpl wfzGzjtService;

    //组织成本中心服务层接口
    @Autowired
    private IWfzBaseunitService wfzBaseunitService;

    //人员服务层接口
    @Autowired
    private WfzPersonServiceImpl wfzPersonService;

    //薪酬总表计提工资福利服务层接口
    @Autowired
    private WfzJtgzflServiceImpl wfzJtgzflService;

    @Autowired
    private IWfzBankService wfzBankService;

    @Autowired
    private WfzBankMapper wfzBankMapper;
    @Autowired
    private GzUtil gzUtil;

    @Autowired
    private GzftHelper gzftHelper;
    @Autowired
    private GzjtHelper gzjtHelper;



    @Override
    @Transactional(rollbackFor=Exception.class)
    public int pushGzff(Long billId, StringBuffer stringBuffer) throws DataNullException {
        //设置一个标识表示出现异常
        int iserror = 0;
        //首先根据传入的单据id，查找对应的数据
        TWfzRemuneration tWfzRemuneration = tWfzRemunerationMapper.selectTWfzRemunerationByFid(billId);
        List<TWfzRemunerationentry> tWfzRemunerationentryList = tWfzRemuneration.getTWfzRemunerationentryList();
        ArrayList<WfzGzff> wfzGzffs = new ArrayList<>();
        gzUtil.splitBill(tWfzRemuneration,tWfzRemunerationentryList,wfzGzffs);
        for (WfzGzff wfzGzff : wfzGzffs) {
            wfzGzffService.insertWfzGzff(wfzGzff);
        }

        return 1;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int pushGzft(Long billId, StringBuffer stringBuffer) throws DataNullException {
        //设置一个标识表示出现异常
        TWfzRemuneration tWfzRemuneration = tWfzRemunerationMapper.selectTWfzRemunerationByFid(billId);
        List<TWfzRemunerationentry> tWfzRemunerationentryList = tWfzRemuneration.getTWfzRemunerationentryList();
        ArrayList<WfzGzft> wfzGzfts = new ArrayList<>();
        gzftHelper.splitBill(tWfzRemuneration,tWfzRemunerationentryList,wfzGzfts);
        for (WfzGzft wfzGzft : wfzGzfts) {
            wfzGzftService.insertWfzGzft(wfzGzft);
        }
        return 1;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int pushGzjt(Long billId, StringBuffer stringBuffer) throws DataNullException {
        TWfzRemuneration tWfzRemuneration = tWfzRemunerationMapper.selectTWfzRemunerationByFid(billId);


        WfzJtgzfl wfzJtgzfl = wfzJtgzflService.selectWfzJtgzflByFid(billId);
        List<WfzJtgzflentry> wfzJtgzflentryList = wfzJtgzfl.getWfzJtgzflentryList();

        ArrayList<WfzGzjt> wfzGzjts = new ArrayList<>();
        gzjtHelper.splitBill(wfzJtgzfl,wfzJtgzflentryList,wfzGzjts);
        for (WfzGzjt wfzGzjt : wfzGzjts) {
            wfzGzjtService.insertWfzGzjt(wfzGzjt);
        }
        return 1;
    }








}
